<%= form_for @changeset, @action, [class: "ui form", multipart: true], fn f -> %>
  <%= if @quick do %>
    <input type="hidden" name="quick" value="true" class="js-quick-form">
  <% end %>

  <div class="three fields">
    <div class="field required <%= AdminHelpers.error_class(f, :type) %>">
      <%= label(f, :type, "Type") %>
      <%= select(f, :type, type_options(), class: "ui fluid dropdown", disabled: has_object(f.data)) %>
    </div>

    <div class="field <%= AdminHelpers.error_class(f, :source_id) %>">
      <%= AdminHelpers.label_with_clear(:source_id, "Source") %>
      <div class="ui fluid remote source search selection dropdown <%= if has_object(f.data), do: "disabled" %>">
        <i class="dropdown icon"></i>
        <% source = AdminHelpers.load_from_form(f, NewsSource, :source) %>
        <%= if source do %>
          <%= hidden_input(f, :source_id, value: source.id) %>
          <div class="selected text"><%= source.name %></div>
        <% else %>
          <%= hidden_input(f, :source_id) %>
          <div class="default text">Select Source</div>
        <% end %>
      </div>
      <%= AdminHelpers.error_message(f, :source_id) %>
    </div>

    <div class="field <%= AdminHelpers.error_class(f, :author_id) %>">
      <%= AdminHelpers.label_with_clear(:author_id, "Author") %>
      <div class="ui fluid remote author search selection dropdown <%= if has_object(f.data), do: "disabled" %>">
        <i class="dropdown icon"></i>
        <% author = AdminHelpers.load_from_form(f, Person, :author) %>
        <%= if author do %>
          <%= hidden_input(f, :author_id, value: author.id) %>
          <div class="selected text"><%= author.name %></div>
        <% else %>
          <%= hidden_input(f, :author_id) %>
          <div class="default text">Select Author</div>
        <% end %>
      </div>
      <%= AdminHelpers.error_message(f, :author_id) %>
    </div>
  </div>

  <div class="field required <%= AdminHelpers.error_class(f, :url) %>">
    <%= label(f, :url, "URL") %>
    <%= text_input(f, :url, placeholder: "https://www.youtube.com/watch?v=dQw4w9WgXcQ", disabled: has_object(f.data)) %>
    <%= AdminHelpers.error_message(f, :url) %>
  </div>

<%= if Enum.any?(@similar) do %>
  <div class="field">
    <label>Similar Items</label>
    <div class="ui ordered list">
    <%= for item <- @similar do %>
      <div class="item">
        <%= link item.headline, to: show_or_preview_path(@conn, item) %> (<%= item.status %>)
      <%= if item.logger, do: "logged by #{item.logger.handle}" %>
      </div>
    <% end %>
    </div>
  </div>
<% end %>

  <div class="field required <%= AdminHelpers.error_class(f, :headline) %>">
    <label for="headline">
      Headline
      <span><a href="javascript:void(0);" class="field-action js-style-guide-modal">(style guide)</a></span>
    </label>
    <%= text_input(f, :headline, placeholder: "The 10 best blog post titles", disabled: has_object(f.data)) %>
    <%= AdminHelpers.error_message(f, :headline) %>
  </div>

  <div class="field <%= AdminHelpers.error_class(f, :story) %>">
    <%= label(f, :story) %>
    <%= textarea(f, :story, placeholder: "Markdown enabled.", rows: 5, disabled: has_object(f.data)) %>
    <%= AdminHelpers.error_message(f, :story) %>
  </div>

  <div class="field">
    <label for="image">
      Image
      <%= AdminHelpers.help_icon("GIF, PNG, or JPG. Ideally >= 600px wide.") %>
      <%= AdminHelpers.file_toggle_buttons() %>
    </label>
    <%= file_input(f, :image) %>
    <%= AdminHelpers.error_message(f, :image) %>

  <%= if f.data.image do %>
    <img src="<%= image_url(f.data, :large) %>" style="width: 100%;">
  <% end %>

  <%= if assigns[:images] do %>
    <div class="ui small images js-image-options" data-options="<%= Jason.encode!(@images) %>"></div>
  <% end %>
  </div>

  <div class="field">
    <%= label(f, :topics) %>

  <%= if has_object(f.data) do %>
    <div class="ui horizontal selection list">
      <%= inputs_for f, :news_item_topics, fn i -> %>
        <% topic = AdminHelpers.load_from_form(i, Topic, :topic) %>
        <div class="item">
          <div class="content"><a class="ui label"><%= topic.name %></a></div>
        </div>
      <% end %>
    </div>
  <% else %>
    <div class="ui horizontal selection list js-news_item_topics">
      <%= inputs_for f, :news_item_topics, fn i -> %>
        <% topic = AdminHelpers.load_from_form(i, Topic, :topic) %>
        <div class="item <%= if AdminHelpers.is_persisted(i.data), do: "persisted" %>">
          <%= hidden_input(i, :topic_id) %>
          <%= hidden_input(i, :position, class: "js-position") %>

          <div class="content">
            <a class="ui label">
              <%= topic.name %>
              <span class="js-remove">
                <i class="delete icon"></i>
                <%= hidden_input(i, :delete) %>
              </span>
            </a>
          </div>
        </div>
      <% end %>
    </div>

    <div class="ui search">
      <div class="ui left icon input">
        <input class="prompt" type="text" placeholder="Add a Topic">
        <i class="block layout icon"></i>
      </div>
    </div>
  <% end %>
  </div>

  <div class="two fields">
    <div class="field required <%= AdminHelpers.error_class(f, :logger_id) %>">
      <%= label(f, :logger_id, "Logged By") %>
      <div class="ui fluid remote logger search selection dropdown <%= if has_object(f.data), do: "disabled" %>">
        <i class="dropdown icon"></i>
        <% logger = AdminHelpers.load_from_form(f, Person, :logger) %>
        <%= if logger do %>
          <%= hidden_input(f, :logger_id, value: logger.id) %>
          <div class="selected text"><%= logger.name %></div>
        <% else %>
          <%= hidden_input(f, :logger_id) %>
          <div class="default text">Select Changelogger</div>
        <% end %>
      </div>
      <%= AdminHelpers.error_message(f, :logger_id) %>
    </div>

    <div class="field <%= AdminHelpers.error_class(f, :submitter_id) %>">
      <%= AdminHelpers.label_with_clear(:submitter_id, "Submitted By") %>
      <div class="ui fluid remote submitter search selection dropdown <%= if has_object(f.data), do: "disabled" %>">
        <i class="dropdown icon"></i>
        <% submitter = AdminHelpers.load_from_form(f, Person, :submitter) %>
        <%= if submitter do %>
          <%= hidden_input(f, :submitter_id, value: submitter.id) %>
          <div class="selected text"><%= submitter.name %></div>
        <% else %>
          <%= hidden_input(f, :submitter_id) %>
          <div class="default text">Select Submitter</div>
        <% end %>
      </div>
      <%= AdminHelpers.error_message(f, :submitter_id) %>
    </div>
  </div>

  <div class="inline field">
    <div class="ui toggle checkbox">
      <%= checkbox(f, :pinned, class: "hidden") %>
      <%= label(f, :pinned) %>
    </div>
  </div>

  <div class="<%= if is_nil(f.data.published_at), do: "hidden" %> js-published-at field <%= AdminHelpers.error_class(f, :published_at) %>">
    <%= label(f, :published_at, "Publish Time") %>
    <%= AdminHelpers.semantic_calendar_field(f, :published_at) %>
    <%= AdminHelpers.error_message(f, :published_at) %>
  </div>

  <div class="ui hidden divider"></div>

<%= if NewsItem.is_published(f.data) || NewsItem.is_queued(f.data) do %>
  <div class="ui equal width stackable grid">
    <div class="column"><%= AdminHelpers.submit_button(:primary, "Save", SharedHelpers.current_path(@conn)) %></div>
    <div class="column"><%= AdminHelpers.submit_button(:secondary, "Save and Close", AdminHelpers.next_param(@conn, Routes.admin_news_item_path(@conn, :index))) %></div>
    <div class="column">
    <%= if NewsItem.is_queued(f.data) do %>
      <button class="ui green fluid basic button" type="submit" name="queue" value="publish">Publish</button>
    <% end %>
    <%= if NewsItem.is_published(f.data) do %>
      <%= link("Unpublish", to: Routes.admin_news_item_path(@conn, :unpublish, f.data), class: "ui red fluid basic button", method: :post, data: [confirm: "Are you sure?"]) %>
    <% end %>
    </div>
  </div>
<% else %>
  <div class="ui equal width stackable grid">
    <div class="column"><button class="ui primary   fluid basic button" type="submit" name="queue" value="draft">Save</button></div>
    <div class="column">
      <div class="ui fluid secondary basic buttons">
        <button class="ui button" type="submit" name="queue" value="append" style="padding-left: 60px; padding-right: 40px;">Publish</button>
        <div class="ui icon dropdown button">
          <i class="angle down icon"></i>
          <div class="menu">
            <button class="link item" type="submit" name="queue" value="publish">Publish Now</button>
            <button class="link item" type="submit" name="queue" value="prepend">Publish Next</button>
          </div>
        </div>
      </div>
    </div>
    <div class="column"><button class="ui secondary fluid basic button js-schedule" type="submit" name="queue" value="append">Schedule</button></div>
  </div>
<% end %>
  </div>
<% end %>

<%= render("_style_guide_modal.html", assigns) %>
